package com.deneme;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.blobstore.BlobstoreService;
import com.google.appengine.api.blobstore.BlobstoreServiceFactory;
import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

public class DeletePuzzle extends HttpServlet {
    private static BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
	
    public void doGet(HttpServletRequest req,
			HttpServletResponse resp)
					throws IOException {
    	doPost(req, resp);
    }
    
    public void doPost(HttpServletRequest req,
			HttpServletResponse resp)
					throws IOException {
    	
    	// Obtain the User's data
    	UserService userService = UserServiceFactory.getUserService();
		User user = userService.getCurrentUser();
		UserData userData = UserData.getDataForUser(user);
		
		// Get the puzzle-key to delete, as well as the user's puzzles.
		String incomingKey = req.getParameter("key");
		ArrayList<String> puzzKeys = userData.getPuzzleKeys();
		
		// Remove the puzzle from the user's list, and save the data
		puzzKeys.remove(incomingKey);
		userData.setPuzzleKeys(puzzKeys);
		userData.save();
		
		// Forward back to the viewPuzzles page
		RequestDispatcher rd = req.getRequestDispatcher("/viewpuzzles");
		try {
			rd.forward(req, resp);
		} catch (ServletException e) {
			e.printStackTrace();
		}
		
    }
}
